package com.itheima.loginTest;

import com.itheima.loginMapper.User;
import com.itheima.loginMapper.UserMapper;
import com.itheima.utils.MyBatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: yg
 * @create 2025-02-07 - 19:41
 * @
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受用户名密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);
        //2.4 调用方法
        User user = mapper.selectByUser(username, password);
        //2.5 释放资源
        MyBatisUtils.closeSession(mapper);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        if(user != null){
            pw.write("登陆成功");
        }else{
            pw.write("登陆失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
